﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Enterprise.Model.Meeting;
using Enterprise.Data.Meeting;
using Enterprise.Infrastructure;
namespace Enterprise.Service.Meeting
{
    /// <summary>
    /// 会议服务类
    /// </summary>
    public class MeetingService
    {

        /// <summary>
        /// 用户缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-Meeting";


        /// <summary>
        /// 内容显示
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public MeetingEntity MeetingDisp(int meetingId)
        {
            return MeetingList().Where(p => p.MeetingId == meetingId).FirstOrDefault();
        }

        /// <summary>
        ///  会议数据列表
        /// </summary>
        /// <returns></returns>
        public List<MeetingEntity> MeetingList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<MeetingEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                MeetingData data = new MeetingData();
                List<MeetingEntity> list = data.MeetingList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 会议 添加、修改、删除
        /// </summary>
        /// <param name="entity">组织机构实体</param>
        public void MeetingExecute(MeetingEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into MEETING_INFO(MEETINGID,MDEPARTMENT,MUSER,MADDRESS,MTIME,MZCR,MTITLE,MTITLERU,MCONTENT,MCONTENTRU,MFILES,MUSERS,MUSERID,MCREATETIME) values(SEQ_MEETING_INFO.nextval," + entity.MDepartMent + "," + entity.MUser + ",'" + entity.MAddress + "'," + Utility.FormatOracleDate(entity.MTime) + ",'" + entity.MZcr + "','" + entity.MTitle + "','" + entity.MTitleRu + "',:MContent,:MContentRu,'" + entity.MFiles + "','" + entity.MUsers + "'," + entity.MUserId + "," + Utility.FormatOracleDate(entity.MCreateTime) + ")";
                    break;
                case "Update":
                    cmd = "Update MEETING_INFO set MADDRESS='" + entity.MAddress + "',MTIME=" +Utility.FormatOracleDate( entity.MTime) + ",MZCR='" + entity.MZcr + "',MTITLE='" + entity.MTitle + "',MTITLERU='" + entity.MTitleRu + "',MCONTENT=:MContent,MCONTENTRU=:MContentRu,MFILES='" + entity.MFiles + "' where MEETINGID=" + entity.MeetingId;
                    break;
                case "Delete":
                    cmd = "Delete from MEETING_INFO where MEETINGID=" + entity.MeetingId;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                if (entity.DB_Option_Action == "Delete")
                {
                    MeetingExecuteBySql(cmd);
                }
                else
                {
                    MeetingExecuteBySqlCommand(cmd, "MContent", entity.MContent, "MContentRu", entity.MContentRu);
                }
            }
        }

        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 通知公告操作
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void MeetingExecuteBySql(string cmd)
        {
            MeetingData data = new MeetingData();
            data.MeetingExecute(cmd);
            ClearCache();
        }

        /// <summary>
        /// 通知公告操作
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void MeetingExecuteBySqlCommand(string cmd, string item1, object item1Value, string item2, object item2Value)
        {
            MeetingData data = new MeetingData();
            data.MeetingExecute(cmd, item1, item1Value, item2, item2Value);
            ClearCache();
        }

        /// <summary>
        ///  会议未签收列表
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public List<MeetingEntity> MeetingUnCheck(int UserId)
        {
            var list=MeetingList();
            MeetingRecevieService mrService=new MeetingRecevieService();
            var q = (from p in list
                     join pp in mrService.MeetingRecevieList() on p.MeetingId equals pp.MID
                     select p).ToList();
            return list.Except(q).ToList();
        }

    }
}
